package com.yuanluludev.lib_base.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.yuanluludev.lib_base.R
import com.yuanluludev.lib_utils.util.ColorUtils

/**
 * 不带网络的底部弹出弹窗的基类
 * yuanLuLu
 */
abstract class BaseBottomSheetDialogFragment : BottomSheetDialogFragment() {

    /**
     * 是否需要使用DataBinding 供子类修改，用户请慎动
     */
    private var isUseDB = false
    protected var behavior: BottomSheetBehavior<View>? = null
    protected var mContext: Context? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isTransBac()) setStyle(STYLE_NORMAL, R.style.TransBottomSheetDialogStyle)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return if (!isUseDB) {
            inflater.inflate(getLayoutResId(), container, false)
        } else {
            initViewModel()
            initDataBinding(inflater, container)?.root
        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dealCommonEvent(view)
        initEvents()

    }

    private fun dealCommonEvent(view: View) {
        (view.findViewById<View?>(R.id.dismissDialog))?.setOnClickListener {
            dismiss()
        }
    }

    open fun initEvents() {}

    /**
     * 是否没有暗黑色背景(默认有暗黑色背景),子类可以继承
     */
    open fun isTransBac(): Boolean = false

    /**
     * 是否可以拖拽取消弹框(默认不可以),子类可以继承
     */
    open fun isCanDrag(): Boolean = false

    abstract fun getLayoutResId(): Int

    fun useDataBinding(isUseDB: Boolean) {
        this.isUseDB = isUseDB
    }

    /**
     * 让子类初始化DataBinding
     */
    open fun initDataBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): ViewDataBinding? = null

    /**
     * 让子类初始化ViewModel
     */
    open fun initViewModel() {}

    /**
     * 获取dialog高度 占整个屏幕的占比(默认自适应高度)
     */
    open fun getDialogHeight(): Int = ViewGroup.LayoutParams.WRAP_CONTENT

    override fun onStart() {
        super.onStart()
        val bottomSheet: View = (dialog as BottomSheetDialog).delegate
            .findViewById(com.google.android.material.R.id.design_bottom_sheet)
            ?: return
        bottomSheet.apply {
            layoutParams.height = getDialogHeight()
            setBackgroundColor((ColorUtils.getColor(android.R.color.transparent)))
        }
        behavior = BottomSheetBehavior.from(bottomSheet).apply {
            state = BottomSheetBehavior.STATE_EXPANDED
            if (!isCanDrag()) {
                isHideable = false
                addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                    override fun onSlide(bottomSheet: View, slideOffset: Float) {

                    }

                    override fun onStateChanged(bottomSheet: View, newState: Int) {
                        if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                            this@apply.state = BottomSheetBehavior.STATE_EXPANDED
                        }
                    }
                })
            }
        }

    }

    fun setDialogHeight(dialogHeight: Int) {
        val bottomSheet: View =
            (dialog as BottomSheetDialog?)?.delegate?.findViewById(com.google.android.material.R.id.design_bottom_sheet)
                ?: return
        bottomSheet.apply {
            layoutParams.height = dialogHeight
        }
    }

    fun show(manager: FragmentManager, isCancelable: Boolean = true) {
        //避免重复添加
        manager.beginTransaction().remove(this).commit()
        if (!this@BaseBottomSheetDialogFragment.isAdded) {
            this.isCancelable = isCancelable
            super.show(manager, this.javaClass.simpleName)
        }
    }

}